﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MvcAtletIS.Models;
using System.Data;

namespace MvcAtletIS.Areas.MyClub.Controllers
{
    [Authorize(Roles = "Users")]
    public class ContactsController : Controller
    {
        atletisEntities db = new atletisEntities();
        
        // GET: /MyClub/Contacts
        public ActionResult Index()
        {
            var contacts = db.Contacts.Where(c => c.ClubId == User.Identity.Name).OrderBy(c => c.Surname).ToList();
            ViewBag.Clubs = db.Clubs.SingleOrDefault(c => c.ID == User.Identity.Name);
            return View(contacts);
        }

        // GET: /MyClub/Contacts/Create
        public ActionResult Create()
        {
            var clubs = db.Clubs.Where(c => c.ID == User.Identity.Name).ToList();
            ViewBag.ClubId = new SelectList(clubs, "ID", "Name", User.Identity.Name);
            return View();
        }

        // POST: /MyClub/Contacts/Create
        [HttpPost]
        public ActionResult Create(Contacts contacts)
        {
            if (ModelState.IsValid)
            {
                db.Contacts.AddObject(contacts);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            var clubs = db.Clubs.Where(c => c.ID == User.Identity.Name).ToList();
            ViewBag.ClubId = new SelectList(clubs, "ID", "Name", User.Identity.Name);
            return View(contacts);
        }

        // GET: /MyClub/Contacts/Edit/5
        public ActionResult Edit(int id)
        {
            Contacts contacts = db.Contacts.Single(c => c.ID == id);
            if (contacts == null)
            {
                return HttpNotFound();
            }

            var clubs = db.Clubs.Where(c => c.ID == User.Identity.Name).ToList();
            ViewBag.ClubId = new SelectList(clubs, "ID", "Name", User.Identity.Name);
            return View(contacts);
        }

        // POST: /MyClub/Contacts/Edit/5
        [HttpPost]
        public ActionResult Edit(Contacts contacts)
        {
            if (ModelState.IsValid)
            {
                db.Contacts.Attach(contacts);
                db.ObjectStateManager.ChangeObjectState(contacts, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            var clubs = db.Clubs.Where(c => c.ID == User.Identity.Name).ToList();
            ViewBag.ClubId = new SelectList(clubs, "ID", "Name", User.Identity.Name);
            return View(contacts);
        }







    }
}
